﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Data;

namespace ConexionBD
{
    class LibretaBD
    {
        string cadena;
        public string stConsulta;
        string titulo;
        string id_libreta;
        string id_usuario;
        public void agregarLibreta() { }
        public void modificarLibreta() { }

         public void agregarLibreta(string titulo)
         {
            
             MySqlConnection Conexion = new MySqlConnection(); 
             this.cadena = "Server=localhost;DataBase=evernote;uid=root;Password=;";
             Conexion.ConnectionString = cadena;
             Conexion.Open();


             MySqlCommand lastId;
             lastId = new MySqlCommand();
             lastId.Connection = Conexion;
             lastId.CommandText = ("SELECT LAST_INSERT_ID()");

             MySqlCommand myCmd = new MySqlCommand();
             myCmd.Connection = Conexion;
             this.titulo = titulo;

             int usuario = 1;

             string query = "insert into libreta (id_libreta,nombre_libreta,id_usuario) values ('" + lastId.CommandText + "','" + titulo + "','" + usuario + "');";

             try
             {
                 myCmd = new MySqlCommand(query, Conexion);

                 myCmd.ExecuteNonQuery();
                 MessageBox.Show("Se han Ingresado los datos");
                 Conexion.Close();



             }
             catch (Exception ex)
             {

                 MessageBox.Show(ex.Message);

                 return;
             }
         }

         public void modificarLibreta(string opcion1, string titulo)
         {
             MySqlConnection Conexion = new MySqlConnection(); 
             this.cadena = "Server=localhost;DataBase=evernote;uid=root;Password=;";
             Conexion.ConnectionString = cadena;
             Conexion.Open();

             MySqlCommand myCmd = new MySqlCommand();
             myCmd.Connection = Conexion;
             string opcion2 = "'" + opcion1 + "'";


             titulo = "'" + titulo + "'";
             MySqlDataAdapter sql5 = new MySqlDataAdapter("UPDATE libreta SET nombre_libreta = " + titulo + " WHERE nombre_libreta = " + opcion2, Conexion);
             DataTable dt5 = new DataTable();
             sql5.Fill(dt5);
         
         }

         public DataTable modLibBD()
         {
             MySqlConnection Conexion = new MySqlConnection();
             cadena = "Server=localhost;DataBase=evernote;uid=root;Password=;";
             Conexion.ConnectionString = cadena;
             Conexion.Open();

             MySqlDataAdapter sql = new MySqlDataAdapter("Select * from libreta", Conexion);
             DataTable dt = new DataTable();
             sql.Fill(dt);
             return dt;

         }
         public DataTable mostLibretaBD(string nickname)
         {
             nickname = "'" + nickname + "'";
             MySqlConnection Conexion = new MySqlConnection();
             cadena = "Server=localhost;DataBase=evernote;uid=root;Password=;";
             Conexion.ConnectionString = cadena;
             Conexion.Open();
             MySqlDataAdapter sql = new MySqlDataAdapter("Select * from libreta L, usuario U where L.id_usuario = U.id_usuario and U.nickname = " + nickname,Conexion);
             DataTable dt = new DataTable();
             sql.Fill(dt);
             return dt;
         
         }
    }
}